//友链配置
toConfigure()
function toConfigure() {
    $.ajax({
        url:root.sroreinter.listLinkConfig,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        contentType: "application/json",
        data: JSON.stringify({
            "pageNum":1,
            "pageSize":30,
            "linkName":$("#linkName").val(),

        }),
        success: function (data) {
            toConfigurelist(data.configList, 0);
            $('.M-box3').pagination({
                pageCount: data.pageCount,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function (api) {
                    $.ajax({
                        url: root.sroreinter.listLinkConfig,
                        cache: false,  //禁用缓存
                        type: "POST",
                        dataType:'json',
                        contentType: "application/json",
                        data: JSON.stringify({
                            "pageNum":api.getCurrent(),
                            "pageSize":30,
                            "linkName":$("#linkName").val(),

                        }),
                        success: function (result) {
                            toConfigurelist(result.configList);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(XMLHttpRequest);
                        }
                    });
                }
            });

        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}
var dataSource = new Array();
var table;   //table对象
dataSource = [];
function toConfigurelist(data, index){
    dataSource.splice(0,dataSource.length);//清空数组
    if(data !=null && data != ''){
        $.each(data, function (i, field) {
            var rowData = new Array();
            rowData.push(field.sequenceNum);
            rowData.push(field.linkName);
            rowData.push(field.linkUrl);
            rowData.push(field.type);
            rowData.push(field.remark);
            rowData.push(field.createTime );
            rowData.push(field.sequence);
            rowData.push(field.id);
            dataSource.push(rowData);

        });
    }
    initDataTable(dataSource);
    function initDataTable(dataSource) {

        var dttable = $('#toConfiguretable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#toConfiguretable").DataTable({
            // "order": false,
            "autoWidth": false,
            "ordering": false,
            "searching": false,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": false,
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
            },
            data: dataSource,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[0]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 0
                },

                {
                    "render": function (data, type, row, meta) {
                        var val = row[1]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 1
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[2]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 2
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = ""
                        if (row[3] == 10) {
                            val = '前台申请'
                        }else {
                            val = '后台添加'
                        }
                        return val;
                    },
                    "targets":3
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[4]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 4
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[5]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 5
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[6]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 6
                },
                {
                    "render": function (data, type, row, meta) {
                      var  val ='<span class=" powerButton 566"><a onclick="editConfiguration(\'' + row[7] + '\',\'' + row[1] + '\',\'' + row[2] + '\',\'' + row[6] + '\',\'' + row[4] + '\')" style="margin-right: 14px">编辑</a></span><span class=" powerButton 567"><a  style="margin-right: 14px" onclick="deletes(\'' + row[7] + '\')">删除</a> </span>'

                        return val;
                    },
                    "targets": 7
                },

            ]
        });
        //权限控制函数
        power()
    };
}
//删除
function deletes(id) {
    var curInd = layer.confirm(
        '<span>确定删除吗？</span>'+'<br/>', {title:'提示',btn: ['确定', '取消']}, function(index){
            $.ajax({
                url:root.sroreinter.deleteLinkConfig,
                cache: false,  //禁用缓存
                type: "POST",
                dataType:'json',
                contentType: "application/json",
                data: JSON.stringify({
                    "id":id,
                    "operatorId":JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.user_ID,
                    "operatorName":JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name,
                }),
                success: function (data) {
                    if(data.code==0){
                        $.Huimodalalert('删除成功',2000);
                        toConfigure()

                    }else {

                        $.Huimodalalert(data.msg,2000);

                    }
                },
                error: function () {
                    $.Huimodalalert('失败！',2000);
                }
            })
            layer.close(curInd);

        },function(){
            layer.close(curInd);

        });

}
//添加
function newConfiguration() {
    $("#newConfiguration").modal("show");
    $(".addressTitle").html("添加链接");
    //查询已存在的友链配置数量
    $.ajax({
        url: root.sroreinter.countLinkConfig,
        cache: false,  //禁用缓存
        type: "GET",
        data: "",
        success: function (data) {
                if(data.code==0){
                    $("#sequence").val(data.configCount+1)
                }
        }
    });
}
//编辑
function editConfiguration(id,linkName1,linkUrl,sequence,remark) {
    $("#newConfiguration").modal("show");
    $(".addressTitle").html("编辑链接");
    $("#cid").val(id)
    $("#linkName1").val(linkName1)
    $("#linkUrl").val(linkUrl)
    $("#sequence").val(sequence)
    $("#remark").val(remark)

}
//取消
function close_model() {
    $("#newConfiguration").modal("hide");
    $("#cid").val("")
    $("#linkName1").val("")
    $("#linkUrl").val("")
    $("#sequence").val("")
    $("#remark").val("")
}


//确定
function determine() {
    if ($("#linkName1").val()=="") {
        $.Huimodalalert('请填写链接名称！',2000);
        return false;
    }
    if ($("#linkUrl").val()=="") {
        $.Huimodalalert('请填写链接地址！',2000);
        return false;
    }
    if ($("#sequence").val()=="") {
        $.Huimodalalert('请填写顺序！',2000);
        return false;
    }

    $.ajax({
        url:root.sroreinter.saveOrUpdateLinkConfig,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        contentType: "application/json",
        data: JSON.stringify({
            "id":$("#cid").val(),
            "linkName":$("#linkName1").val(),
            "linkUrl":$("#linkUrl").val(),
            "sequence":$("#sequence").val(),
            "remark":$("#remark").val(),
            "operatorId":JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.user_ID,
            "operatorName":JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name,
        }),
        success: function (data) {
            if(data.code==0){
                if($(".addressTitle").html()=="编辑链接"){
                    $.Huimodalalert('编辑成功',2000);
                }else {
                    $.Huimodalalert('添加成功',2000);
                }

                $("#newConfiguration").modal("hide");
                $("#cid").val("")
                $("#linkName1").val("")
                $("#linkUrl").val("")
                $("#sequence").val("")
                $("#remark").val("")
                toConfigure()

            }else {

                $.Huimodalalert(data.msg,2000);

            }
        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    })

}

//友链申请
function apply() {
    $.ajax({
        url:root.sroreinter.listLinkApply,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        contentType: "application/json",
        data: JSON.stringify({
            "pageNum":1,
            "pageSize":30,
            "linkName":$("#linkNames").val(),
            "telephone":$("#telephone").val(),
            "email":$("#email").val(),
            "examStatus":$("#examStatus").val(),

        }),
        success: function (data) {
            applylist(data.linkApplyList, 0);
            $('.M-box3').pagination({
                pageCount: data.pageCount,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function (api) {
                    $.ajax({
                        url: root.sroreinter.listLinkApply,
                        cache: false,  //禁用缓存
                        type: "POST",
                        dataType:'json',
                        contentType: "application/json",
                        data: JSON.stringify({
                            "pageNum":api.getCurrent(),
                            "pageSize":30,
                            "linkName":$("#linkNames").val(),
                            "telephone":$("#telephone").val(),
                            "email":$("#email").val(),
                            "examStatus":$("#examStatus").val(),

                        }),
                        success: function (result) {
                            applylist(result.linkApplyList);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(XMLHttpRequest);
                        }
                    });
                }
            });

        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}
var dataSource2 = new Array();
var table2;   //table对象
dataSource2 = [];
function applylist(data, index){
    dataSource2.splice(0,dataSource2.length);//清空数组
    if(data !=null && data != ''){
        $.each(data, function (i, field) {
            var rowData = new Array();
            rowData.push(field.sequenceNum);
            rowData.push(field.linkName);
            rowData.push(field.linkUrl);
            rowData.push(field.telephone);
            rowData.push(field.email);
            rowData.push(field.examStatus );
            rowData.push(field.id);
            dataSource2.push(rowData);

        });
    }
    initDataTable(dataSource2);
    function initDataTable(dataSource2) {

        var dttable = $('#applytable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#applytable").DataTable({
            // "order": false,
            "autoWidth": false,
            "ordering": false,
            "searching": false,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": false,
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
            },
            data: dataSource2,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[0]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 0
                },

                {
                    "render": function (data, type, row, meta) {
                        var val = row[1]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 1
                },

                {
                    "render": function (data, type, row, meta) {
                        var val = row[2]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 2
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[3]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets":3
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[4]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 4
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = ""
                        if (row[5] == 10) {
                            val = '待审核'
                        }else  if (row[5] == 20){
                            val = '已通过'
                        }else  if (row[5] == 30){
                            val = '已拒绝'
                        }
                        return val;
                    },
                    "targets": 5
                },
                {
                    "render": function (data, type, row, meta) {
                        var val ='<span  class=" powerButton 565"><a onclick="viewDetails(\'友链详情\',\'friend_chain_configuration_details.html\',\''+row[6]+'\')">查看详情</a></span>'
                        return val;
                    },
                    "targets": 6
                },


            ]
        });
        //权限控制函数
        power()
    };
}
//查看详情
function viewDetails(title,url,id) {
    sessionStorage.setItem("id",id)
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}
